/*
 * UMLSynch plugin for Eclipse
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.dialogs;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.googlecode.umlsynch.eclipse.rpc.Debug.TEclipseDebug;


/**
 * This class demonstrates how to create your own dialog classes. It allows users
 * to input a String
 */
public class FileSelectDialog extends Dialog {
  private String[] fileList;
  private String slectedFile;
  private TEclipseDebug debug = new TEclipseDebug("FileSelectDialog");

  /**
   * InputDialog constructor
   * 
   * @param parent the parent
   */
  public FileSelectDialog(Shell parent) {
    // Pass the default styles here
    this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
  }

  /**
   * InputDialog constructor
   * 
   * @param parent the parent
   * @param style the style
   */
  public FileSelectDialog(Shell parent, int style) {
    // Let users override the default styles
    super(parent, style);
    setText("Please choose the File:");    
  }

  /**
   * Gets the message
   * 
   * @return String
   */
  public String[] getFileList() {
    return fileList;
  }

  /**
   * Sets the message
   * 
   * @param message the new message
   */
  public void setFileList(String[] flist) {
    this.fileList = flist.clone();
  }

  /**
   * Gets the input
   * 
   * @return String
   */
  public String getSelectedFile() {
    return this.slectedFile;
  }

  /**
   * Sets the input
   * 
   * @param input the new input
   */
  public void setSelectedFile(String sfile) {
    this.slectedFile = sfile;
  }

  /**
   * Opens the dialog and returns the input
   * 
   * @return String
   */
  public String open() {
    // Create the dialog window
	
    Shell shell = new Shell(getParent(), getStyle());
    
    shell.setText(getText());
    
    createContents(shell);
    
    shell.pack();
    
    shell.open();
    
    Display display = getParent().getDisplay();
    
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    // Return the entered value, or null
    return slectedFile;
  }

  /**
   * Creates the dialog's contents
   * 
   * @param shell the dialog window
   */
  private void createContents(final Shell shell) {
    shell.setLayout(new GridLayout(2, true));
    
    // Show the message
    Label label = new Label(shell, SWT.NONE);
    label.setText("Please choose the file:");
    
    GridData data = new GridData();
    
    data.horizontalSpan = 2;
    
    label.setLayoutData(data);

    // Display the input box
    final Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION| SWT.CHECK);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("");
    
    TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("filename");

    if (fileList != null && fileList.length > 0)
    {
    	for (int i = 0; i < fileList.length; i++) {
    		TableItem item = new TableItem(table, SWT.NONE);
    		item.setText(0, "");
    		item.setText(1, fileList[i]);    		
    	}

    	for (int i=0; i<2; i++) {
    		table.getColumn (i).pack ();
    	}     
    }  

    table.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    
    Button ok = new Button(shell, SWT.PUSH);
    ok.setText("OK");
    
    data = new GridData(GridData.FILL_HORIZONTAL);
    ok.setLayoutData(data);
    ok.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        
    	int checkCount = 0;
    	for (int i=0; i<table.getItemCount(); i++)
    		if (table.getItems()[i].getChecked())
    			checkCount++;

    	if (checkCount == 1)
    	{
    		slectedFile = new String();
    		checkCount = 0;

    		for (int i=0; i<table.getItemCount(); i++)
    			if (table.getItems()[i].getChecked()) {
    				slectedFile = fileList[i];    				
    			}
    	}	   
    	
        shell.close();
      }
    });

    Button cancel = new Button(shell, SWT.PUSH);
    cancel.setText("Cancel");
    data = new GridData(GridData.FILL_HORIZONTAL);
    cancel.setLayoutData(data);
    cancel.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        slectedFile = null;
        shell.close();
      }
    });

    // Set the OK button as the default, so
    // user can type input and press Enter
    // to dismiss
    debug.Note("button ok is default ");
    shell.setDefaultButton(ok);
  }
}
